package log

import (
	"encoding/json"
	"fmt"
	"runtime"
	"time"
)

func LogUserM(userId interface{}, info string, attach map[string]interface{}) {

	_, f, l, _ := runtime.Caller(1)
	loc := fmt.Sprintf("%s:%d", f, l)

	var now = time.Now().Format("2006-01-02 15:04:05.000")
	var m = map[string]interface{}{
		"global":     globalArgs,
		"user_id":    userId,
		"info":       info,
		"stack":      loc,
		"created_at": now,

		"level": "INFO",
	}

	for k, v := range attach {
		m[k] = v
	}

	jsonline, _ := json.Marshal(m)

	globalStore.write(string(jsonline))
}

func ErrorUserM(userId interface{}, info string, attach map[string]interface{}) {

	_, f, l, _ := runtime.Caller(1)
	loc := fmt.Sprintf("%s:%d", f, l)

	var now = time.Now().Format("2006-01-02 15:04:05.000")
	var m = map[string]interface{}{
		"global":     globalArgs,
		"user_id":    userId,
		"info":       info,
		"stack":      loc,
		"created_at": now,

		"level": "ERROR",
	}

	for k, v := range attach {
		m[k] = v
	}

	jsonline, _ := json.Marshal(m)

	globalStore.write(string(jsonline))
}

func logM(info string, attach map[string]interface{}, depth int) {

	_, f, l, _ := runtime.Caller(depth)
	loc := fmt.Sprintf("%s:%d", f, l)

	var now = time.Now().Format("2006-01-02 15:04:05.000")

	var m = map[string]interface{}{
		"global":     globalArgs,
		"info":       info,
		"stack":      loc,
		"created_at": now,
		"level": "INFO",

	}

	for k, v := range attach {
		m[k] = v
	}

	jsonline, _ := json.Marshal(m)

	globalStore.write(string(jsonline))
}

func LogM(info string, attach map[string]interface{}) {
	logM(info, attach, 2)
}

func Infof(format string, args ...interface{}) {
	info := fmt.Sprintf(format, args ...)

	var now = time.Now().Format("2006-01-02 15:04:05.000")
	_, f, l, _ := runtime.Caller(1)
	loc := fmt.Sprintf("%s:%d", f, l)
	var m = map[string]interface{}{
		"global":     globalArgs,
		"info":       info,
		"stack":      loc,
		"created_at": now,
		"level": "INFO",

	}

	jsonline, _ := json.Marshal(m)

	globalStore.write(string(jsonline))

}


func Errorf(format string, args ...interface{}) {
	info := fmt.Sprintf(format, args ...)

	var now = time.Now().Format("2006-01-02 15:04:05.000")
	_, f, l, _ := runtime.Caller(1)
	loc := fmt.Sprintf("%s:%d", f, l)
	var m = map[string]interface{}{
		"global":     globalArgs,
		"info":       info,
		"stack":      loc,
		"created_at": now,
		"level": "ERROR",

	}

	jsonline, _ := json.Marshal(m)

	globalStore.write(string(jsonline))

}

